package com.prototype.ECE492;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;

public class MenuActivity extends Activity {
	SharedPreferences menuSettings;
	SharedPreferences.Editor prefEditor;
	
	ArrayList<String> tagTypes;
	ArrayList<Boolean> tagStates;
	
//	Spinner spinner;
//	ArrayAdapter<CharSequence> adapter;
	
//	ViewGroup allViews;
	
	
	public void onCreate(Bundle savedInstancestate){
		super.onCreate(savedInstancestate);
		menuSettings = getSharedPreferences("MenuPref", MODE_PRIVATE);
		prefEditor = menuSettings.edit();
		setContentView(R.layout.settings);
		tagTypes = new ArrayList<String>();
		tagStates = new ArrayList<Boolean>();
		getStrings();
		configureCheckBoxes();
		configureSpinner();
		
		Button backButton = (Button) findViewById(R.id.backButton);
		backButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				setResult(RESULT_OK, new Intent());
				finish();
			}
		});
	}
	
	void getStrings(){
		InputStream tagsFile = getBaseContext().getResources().openRawResource(R.raw.tagtypes);
		BufferedReader readTags = new BufferedReader(new InputStreamReader(tagsFile));
		try{
			String typeLine;
			while((typeLine = readTags.readLine()) != null){
				StringTokenizer parse = new StringTokenizer(typeLine,",");
				String typeName = parse.nextToken();
				tagTypes.add(typeName);
				tagStates.add(menuSettings.getBoolean(typeName,true));	
			}
		}catch(IOException e){}
	}
	
	void configureCheckBoxes(){
		ViewGroup checkBoxLayout = (ViewGroup)findViewById(R.id.checkBoxLayout);
		int currentTypeIndex = 0;
		for (String tagTypeName : tagTypes){
			MyCheckBox checkBox = new MyCheckBox(this, currentTypeIndex);
			checkBox.setText(tagTypeName);
			checkBoxLayout.addView(checkBox);
			currentTypeIndex++;
		}
	}
	
	void configureSpinner(){
		Spinner spinner = (Spinner) findViewById(R.id.selectSpinner);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,
				tagTypes);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(menuSettings.getInt("COLOR_CHOICE", 0));
		spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
				prefEditor.putInt("COLOR_CHOICE",pos);
				prefEditor.commit();
			}
			public void onNothingSelected(AdapterView<?> parent){
				// do nothing
			}
		});
	}

	
	class MyCheckBox extends CheckBox{
		int index;
		
		MyCheckBox(Context c, int i){
			super(c);
			index = i;
			setChecked(tagStates.get(index));
			setOnClickListener(new View.OnClickListener(){
				public void onClick(View view){
					boolean state = !tagStates.get(index);
					tagStates.set(index, state);
					prefEditor.putBoolean(tagTypes.get(index),state);
					prefEditor.commit();
				}
			});
		}
		
		public void setIndex(int index){
			this.index=index;
		}
		public int getIndex(){
			return index;
		}
	}
}
